package com.example.xml_test;

import java.io.IOException;
import java.io.StringReader;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;

import android.os.Bundle;
import android.app.Activity;
import android.content.res.Resources.NotFoundException;
import android.text.method.ScrollingMovementMethod;
import android.util.Log;
import android.view.Menu;
import android.widget.TextView;

public class MainActivity extends Activity {
	private TextView textView1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		initView();
		initHandler();
	}

	private void initHandler() {
		// TODO Auto-generated method stub
	}
	
	private void initView() {
		// TODO Auto-generated method stub
		textView1 = (TextView) findViewById (R.id.textView1);
		String content = getXML ();
		textView1.setText(content);
		textView1.setMovementMethod(ScrollingMovementMethod.getInstance());
	}

	private String getXML2() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		try {
			DocumentBuilder parser = factory.newDocumentBuilder();
			try {
				Document doc = parser.parse(getResources().openRawResource(R.raw.cities2 ));
				Element root = doc.getDocumentElement();
				NodeList parentlist= root.getElementsByTagName("GetCitiesByCountryResult");
				String xmlcontent = ((Element) parentlist.item(0)).getTextContent();
				
				NodeList list = getNodeList(xmlcontent);
				for(int i = 0; i<parentlist.getLength(); i++){
					Element ele = (Element) parentlist.item(i );
					String country =((Element)ele.getElementsByTagName("Country").item(0)).getTextContent();
					Element cityEle = (Element)ele.getElementsByTagName("City").item(0);
					String city = cityEle.getTextContent();
					sb.append( "CDATA : "+ i +": "+country+ "==>"+city +"\n");
					
				}
			} catch (NotFoundException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (SAXException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
		return sb.toString();
	}

	
	private NodeList getNodeList(String xmlcontent) {
		// TODO Auto-generated method stub
		NodeList lis = null;
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		try {
			DocumentBuilder parser = factory.newDocumentBuilder();
			InputSource is = new InputSource();
			is.setCharacterStream(new StringReader(xmlcontent));
			Document doc = parser.parse(is);
			lis = doc.getElementsByTagName("Table");

		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return lis;
	}

	private String getXML() {
		// TODO Auto-generated method stub
		StringBuilder sb = new StringBuilder();
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
		
		try {
			DocumentBuilder parser = factory.newDocumentBuilder();
			Document doc = parser.parse(getResources().openRawResource(R.raw.cities));
			Element root = doc.getDocumentElement();
			NodeList list = root.getElementsByTagName("Table");
			for(int i = 0; i< list.getLength();i++){
				Element ele = (Element)list.item(i);//<Table>
				String country =  ((Element)ele.getElementsByTagName("Country").item(0)).getTextContent();//<Country>
				Element cityEle = (Element)ele.getElementsByTagName("City").item(0);
				String city = cityEle.getTextContent();
				sb.append(i+": "+country+" ==> "+city+"\n");
			}
			Log.i ("DOM","COUNT : "+list.getLength());
		} catch (ParserConfigurationException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (NotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SAXException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return sb.toString();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

}
